package com.kun.product.service.impl;

import com.kun.product.entity.ProductEntity;
import com.kun.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.Query;

import com.kun.product.dao.ClassifyDao;
import com.kun.product.entity.ClassifyEntity;
import com.kun.product.service.ClassifyService;
import org.springframework.util.StringUtils;


@Service("classifyService")
public class ClassifyServiceImpl extends ServiceImpl<ClassifyDao, ClassifyEntity> implements ClassifyService {
    @Autowired
    ProductService productService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ClassifyEntity> queryWrapper = new QueryWrapper<>();
        String classify = (String) params.get("key");
        if (!StringUtils.isEmpty(classify)){
            queryWrapper.like("classify",classify);
        }
        IPage<ClassifyEntity> page = this.page(
                new Query<ClassifyEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public boolean removeClassifyByIds(List<Integer> asList) {

       for (Integer i:asList){
           QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
           queryWrapper.eq("cid",i).or();
           long count = productService.count(queryWrapper);
           if (count>0){
               return false;
           }
       }
        baseMapper.deleteBatchIds(asList);
       return true;

    }

}